<?php
	
	/**
	* 
	*/
	class carpicModel{

		public $_table = 'carpic';//设置表名

		//取一组图片信息
		public function getcarpic($carpic){
			if(!empty($carpic)){
				$sql = 'SELECT * FROM '.$this -> _table.' WHERE id IN ('.$carpic.')';
				return DB::findAll($sql);
			}else{
				return array();
			}
		}

		//图片验证程序
		public function carpicsubmit(){
			
			//更新图片信息、返回修改记录
			$this -> carupdatepic();
			
//			存储图片信息、返回存储id
			$carpic = $this -> carholdpic();
//			for($j=0;$j<count($_POST['note']);$j++){
//				$carids[] = $picid + $j;
//			}
//			$carpic = implode(",", $carids);
			
			//
			if($_GET['carid'] != 'x'){
				if($carnum == 0){
					return M('carinfo') -> updatacarid($carpic, $_GET['carid']);
				}else{
					return M('carinfo') -> updatacarid(','.$carpic, $_GET['carid']);
				}
			}else{
				return M('carinfo') -> setcarid($carpic);
			}
		}
		
		//图片添加存储
		public function carholdpic(){
			//下载并存储图片
			if( count($_POST['note']) == 0 ) return;
			for($i=0;$i<count($_POST['note']);$i++){
				$note[$i] = isset($_POST['note'][$i])? $_POST['note'][$i] : '';
				$picurl[$i] = "static/image/carimg/".$_FILES['file']["name"][$i];
				move_uploaded_file($_FILES['file']["tmp_name"][$i],$picurl[$i]);
				
				$valueArr[] = "('".$note[$i]."','".$picurl[$i]."')";
			}     
			$value = implode(",", $valueArr);  
			$sql = "insert ".$this -> _table."( `speak`,`picurl`) values".$value;
			
			DB::query($sql);
			$picid = mysql_insert_id();
			
			for($j=0;$j<count($_POST['note']);$j++){
				$carids[] = $picid + $j;
			}
			return implode(",", $carids);
		}
		
		//图片信息修改
		public function carupdatepic(){
			//更新存储图片信息
			for($i=0;$i<count($_POST['carpicid']);$i++){
				$upnote[$i] = isset($_POST['upnote'][$i])? $_POST['upnote'][$i] : '';
				$updatanote[$i] = isset($_POST['updatanote'][$i])? $_POST['updatanote'][$i] : '';
				$carpicid[$i] = isset($_POST['carpicid'][$i])? $_POST['carpicid'][$i] : '';
				
				if( $upnote[$i] !== $updatanote[$i] ){
					$sql = "update ".$this -> _table." set speak = '".$updatanote[$i]."' where id = ".$carpicid[$i];
					DB::query($sql);
				}
			}   
			return count($_POST['carpicid']);
		}
		
		//查找所有图片信息
		function findAll_orderby_dataline(){
			$sql = 'select * from '.$this -> _table;
			return DB::findAll($sql);
		}

		//删除图片信息
		function del_by_id($carpicid, $carid){
			DB::del($this -> _table, 'id='.intval($carpicid));
			return M('carinfo') -> delcarid($carpicid, $carid);
		}


	}
?>